#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compar_int(const void *e1, const void *e2)
{
    return (*(int *)e1 > *(int *)e2) - (*(int *)e1 < *(int *)e2);
}
void swap(char *e1, char *e2, size_t size)
{
    for (size_t i = 0; i < size; i++)
    {
        char tmp = 0;
        tmp = *e1;
        *e1 = *e2;
        *e2 = tmp;
        ++e1;
        ++e2;
    }
}
void bubble_sort(void *base, size_t num, size_t size, int (*compar)(const void *e1, const void *e2))
{
    // 冒泡排序的趟数
    for (size_t i = 0; i < num - 1; i++)
    {
        int flag = 1;
        // 一趟冒泡排序
        for (size_t j = 0; j < num - 1 - i; j++)
        {
            if (compar((char *)base + j * size, (char *)base + (j + 1) * size) > 0)
            {
                // 交换
                flag = 0;
                swap((char *)base + j * size, (char *)base + (j + 1) * size, size);
            }
        }
        if (flag == 1)
        {
            break;
        }
    }
}
void print_sort(int arr[], int num)
{
    for (size_t i = 0; i < num; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void test1()
{
    int arr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    int num = sizeof(arr) / sizeof(arr[0]);
    int size = sizeof(arr[0]);
    print_sort(arr, num);
    bubble_sort(arr, num, size, compar_int);
    print_sort(arr, num);
}

struct Stu
{
    char name[20];
    int age;
};

int cmp_stu_by_age(const void *e1, const void *e2)
{
    return ((struct Stu *)e1)->age - ((struct Stu *)e2)->age;
}

int cmp_stu_by_name(const void *e1, const void *e2)
{
    return strcmp(((struct Stu *)e1)->name, ((struct Stu *)e2)->name);
}
void test2()
{
    struct Stu arr[] = {{"zhngsan", 20}, {"lisi", 30}, {"wangwu", 15}};
    int num = sizeof(arr) / sizeof(arr[0]);
    bubble_sort(arr, num, sizeof(arr[0]), cmp_stu_by_name);
}
void test3()
{
    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
    printf("%d\n", strlen(arr));
}
int main()
{
    // test1();
    // test2();
    test3();
    return 0;
}